package pers.kevin.client;

import java.io.IOException;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.dom4j.Document;
import org.dom4j.io.SAXReader;

public class HttpsClient {

	public static void main(String[] args) throws IOException {
		HttpsClient t=new HttpsClient();
		String sURL = "https://localhost:8443/https2tcp/servlet/test";
		String reqData = "<root><name>测试人</name><age>26</age><addr></addr></root>"
		
		String response = t.toHTTPS(sURL, xml);
		System.out.println("接收到返回数据："+response);
	}

	/**
	 * https调用 <功能详细描述>
	 * 
	 * @param sURL
	 *            请求URL
	 * @param xml
	 *            请求报文
	 * @return
	 * @throws IOException
	 * @see [类、类#方法、类#成员]
	 */
	public String toHTTPS(String sURL, String xml) throws IOException {

		System.setProperty("javax.net.ssl.trustStore",
						"D:\\workspace\\https2tcp_v1.0\\client.keystore");
		System.setProperty("javax.net.ssl.trustStorePassword", "123456");
		System.setProperty("javax.net.ssl.keyStoreType", "PKCS12");
		System.setProperty("javax.net.ssl.keyStore",
						"D:\\workspace\\https2tcp_v1.0\\client.pfx");
		System.setProperty("javax.net.ssl.keyStorePassword", "123456");

		String resultXML = "";
		HttpClient httpClient = null;
		PostMethod postMethod = null;
		try {
			httpClient = new HttpClient();
			// 设置超时时间
			httpClient.getHttpConnectionManager().getParams()
					.setConnectionTimeout(30000);
			httpClient.getHttpConnectionManager().getParams().setSoTimeout(
					30000);
			postMethod = new PostMethod(sURL);
			postMethod.setRequestEntity(new StringRequestEntity(xml,
					"text/html", "UTF-8"));

			postMethod.setRequestHeader("Content-Type",
					"text/xml;charset=UTF-8");
			postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
					new DefaultHttpMethodRetryHandler());
			int statusCode = httpClient.executeMethod(postMethod);
			if (statusCode != HttpStatus.SC_OK) {
				System.out.println("Call method failed: "
						+ postMethod.getStatusLine());
			}
			BufferedReader in = null;
			try {
				in = new BufferedReader(new InputStreamReader(postMethod
						.getResponseBodyAsStream(), "utf-8"));
				SAXReader saxReader = new SAXReader();
				Document doc = saxReader.read(new InputStreamReader(postMethod
						.getResponseBodyAsStream(), "utf-8"));
				resultXML = doc.asXML();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if (in != null) {
					try {
						in.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (postMethod != null) {
				postMethod.releaseConnection();
			}
		}
		return resultXML;
	}

}